package com.exit490.cfp.activities.capital;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.exit490.cfp.R;
import com.exit490.cfp.adapters.StableArrayAdapter;
import com.exit490.cfp.dao.CapitalDAO;
import com.exit490.cfp.modelo.Capital;

public class CapitalActivity extends ActionBarActivity {

    private Context context = this;
    private ArrayList<Capital> listaDeCapitais = null;
    private ListView listViewCapitais = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capital);
        listarCapitais();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        listarCapitais();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_action_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public void cliqueBotaoAdicionarCapital(MenuItem menuItem) {
        dialogNovoCapital();
    }

    public void dialogNovoCapital() {
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dialog_novo_capital);
        dialog.setTitle("Title...");

        Button dialogBotaoAdicionar = (Button) dialog.findViewById(R.id.dialog_botao_adicionar);
        Button dialogBotaoCancelar = (Button) dialog.findViewById(R.id.dialog_botao_cancelar);

        dialogBotaoAdicionar.setOnClickListener(new OnClickListener() {
            Capital capital = new Capital();
            CapitalDAO capitalDAO = new CapitalDAO(context);

            @Override
            public void onClick(View v) {
                EditText campoNomeCapital = (EditText) dialog.findViewById(R.id.campo_nome_capital);

                if (campoNomeCapital.getText() != null && !"".equals(campoNomeCapital.getText().toString())) {
                    capital.setNomeDoCapital(campoNomeCapital.getText().toString());
                }

                try {
                    capitalDAO.cadastrarCapital(capital);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                dialog.dismiss();
                onResume();
            }
        });

        dialogBotaoCancelar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    public void listarCapitais() {
        CapitalDAO capitalDAO = new CapitalDAO(context);
        listViewCapitais = (ListView) findViewById(R.id.list_view_capitais);
        try {
            listaDeCapitais = capitalDAO.listarTodosCapitais();
        } catch (Exception e) {
            e.printStackTrace();
        }

        final ArrayList<String> nomeDosCapitais = new ArrayList<String>();
        for (Capital capital : listaDeCapitais) {
            nomeDosCapitais.add(capital.getNomeDoCapital());
        }

        final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, nomeDosCapitais);
        listViewCapitais.setAdapter(adapter);

    }
}
